//
//  LoginViewController.m
//  XLZChat
//
//  Created by dllo on 16/10/21.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "LoginViewController.h"
#import "RegistViewController.h"
#import "AVOSCloudIM.h"
#import "AppDelegate.h"
#import "XLZ_Macro.h"
#import "UIViewController+ZHWCategory.h"

@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *userNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor whiteColor];
    
}

- (IBAction)loginButtonClicked:(UIButton *)sender {
    if (_userNameTextField.text.length > 0 & _passwordTextField.text.length > 0) {
        AVUser *user = [AVUser user];
        [self login:user];
    } else {
        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"通知" message:@"请输入用户名或密码" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        }];
        [alertVC addAction:action];
        [self presentViewController:alertVC animated:YES completion:^{
            
        }];
    }
}

- (void)login:(AVUser *)user {
    [AVUser logInWithUsernameInBackground:_userNameTextField.text password:_passwordTextField.text block:^(AVUser *user, NSError *error) {
        if (error == nil) {
            NSLog(@"登录成功");
            AppDelegate *appdelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
            appdelegate.window.rootViewController = appdelegate.drawVC;
        } else {
            if (error.code == 210) {
                [self showMessage:@"用户名或密码错误"];
            }
        }
    }];
}

- (IBAction)registButtonClicked:(UIButton *)sender {
    _userNameTextField.text = nil;
    _passwordTextField.text = nil;
    RegistViewController *registVC = [[RegistViewController alloc] init];
    registVC.passAccount = ^(NSString *userName, NSString *password) {
        _userNameTextField.text = userName;
        _passwordTextField.text = password;
    };
    [self.navigationController pushViewController:registVC animated:YES];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
